﻿using FluentValidation;
using Mes.Domain.BoundedContext;
using Mes.Application.DTO.Inputs.Operations;
using Mes.Application.DTO.Results.Operations;
using Mes.Application.Services.Operations;
using Microsoft.AspNetCore.Mvc;
using Mes.Application.Common;

namespace Mes.Application.Web.Controllers.Operations
{
    /// <summary>
    /// 工艺路线控制器
    /// </summary>
    [ApiController]
    [Route("/api/v1")]
    public class ProcessRouteController : ControllerBase
    {
        private readonly IProcessRouteService _processRouteService;
        private readonly IValidator<AddProcessRouteInput> _addProcessRouteInputValidator;
        private readonly IValidator<GetProcessRoutesInput> _getProcessRoutesInputValidator;
        private readonly IValidator<UpdateProcessRouteInput> _updateProcessRouteInputValidator;

        /// <summary>
        /// 工艺路线控制器
        /// </summary>
        /// <param name="processRouteService"></param>
        /// <param name="addProcessRouteInputValidator"></param>
        /// <param name="getProcessRoutesInputValidator"></param>
        /// <param name="updateProcessRouteInputValidator"></param>
        public ProcessRouteController(IProcessRouteService processRouteService, 
            IValidator<AddProcessRouteInput> addProcessRouteInputValidator,
            IValidator<GetProcessRoutesInput> getProcessRoutesInputValidator,
            IValidator<UpdateProcessRouteInput> updateProcessRouteInputValidator)
        {
            _processRouteService = processRouteService;
            _addProcessRouteInputValidator = addProcessRouteInputValidator;
            _getProcessRoutesInputValidator = getProcessRoutesInputValidator;
            _updateProcessRouteInputValidator = updateProcessRouteInputValidator;
        }


        /// <summary>
        /// 新增工艺路线
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("processroute")]
        public async Task<ActionResult> AddProcessRouteAsync([FromBody] AddProcessRouteInput input)
        {
            try
            {
                var result = await _addProcessRouteInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _processRouteService.AddProcessRouteAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 编辑工艺路线
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPatch("processroute")]
        public async Task<ActionResult> UpdateProcessRouteAsync([FromBody] UpdateProcessRouteInput input)
        {
            try
            {
                var result = await _updateProcessRouteInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _processRouteService.UpdateProcessRouteAsync(input);
                return Ok();
            }
            catch(CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除工艺路线
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("processroute")]
        public async Task<ActionResult> DeleteProcessRoutesAsync(IList<int> ids)
        {
            try
            {
                await _processRouteService.DeleteProcessRoutesAsync(ids);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询工艺路线分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("processroutes")]
        public async Task<ActionResult<PagedItemsData<ProcessRoutesResult>>> GetProcessRoutesAsync([FromQuery] GetProcessRoutesInput input)
        {
            try
            {
                var result = await _getProcessRoutesInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                var response = await _processRouteService.GetProcessRoutesAsync(input);
                return Ok(response);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询工艺路线实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("processroute")]
        public async Task<ActionResult<ProcessRouteResult>> GetProcessRouteAsync(int id)
        {
            try
            {
                var response = await _processRouteService.GetProcessRouteAsync(id);
                return Ok(response);
            }
            catch(CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }
    }
}
